const express = require('express');
const Mock = require('mockjs');
const router = express.Router();

// 获取商品列表
router.get('/', (req, res) => {
  const { page = 1, pageSize = 10, category } = req.query;
  
  const listTemplate = `list|${pageSize}`;
  
  const data = Mock.mock({
    [listTemplate]: [
      {
        'id|+1': (page - 1) * pageSize + 1,
        'name': '@ctitle(3, 8)',
        'price|1-1000.2': 1,
        'originalPrice|1-2000.2': 1,
        'discount|0-95': 1,
        'sales|100-10000': 1,
        'stock|0-1000': 1,
        'category': category || '@pick(["电子产品", "服装鞋帽", "家居用品", "食品饮料", "图书音像"])',
        'images': [
          '@image("300x300", "#50B347", "#FFF", "@word(2, 4)")',
          '@image("300x300", "#C0EB75", "#FFF", "@word(2, 4)")',
          '@image("300x300", "#79B1E5", "#FFF", "@word(2, 4)")'
        ],
        'description': '@cparagraph(1, 3)',
        'createTime': '@datetime'
      }
    ],
    'total|100-500': 1
  });
  
  res.json({
    code: 200,
    message: 'success',
    data
  });
});

// 获取单个商品
router.get('/:id', (req, res) => {
  const { id } = req.params;
  
  const data = Mock.mock({
    'id': +id,
    'name': '@ctitle(5, 10)',
    'price|1-10000.2': 1,
    'originalPrice|1-15000.2': 1,
    'discount|0-1.1': 1,
    'sales|0-10000': 1,
    'stock|0-1000': 1,
    'category': '@pick(["电子产品", "服装鞋帽", "食品饮料", "家居用品", "图书音像"])',
    'images|3-8': ['@image("500x500", "@color", "@cword(2, 4)")'],
    'description': '@cparagraph(3, 5)',
    'specifications': {
      '尺寸|1': ['S', 'M', 'L', 'XL', 'XXL'],
      '颜色|1-3': ['红色', '蓝色', '绿色', '黄色', '黑色', '白色'],
      '材质': '@cword(2, 4)'
    },
    'createTime': '@datetime',
    'reviews|0-50': [
      {
        'id|+1': 1,
        'userId': '@integer(1, 1000)',
        'userName': '@cname',
        'content': '@cparagraph(1, 2)',
        'rating|1-5': 1,
        'createTime': '@datetime'
      }
    ]
  });
  
  res.json(data);
});

// 创建商品
router.post('/', (req, res) => {
  res.json({
    code: 0,
    message: '创建商品成功',
    data: req.body
  });
});

// 更新商品
router.put('/:id', (req, res) => {
  res.json({
    code: 0,
    message: '更新商品成功',
    data: req.body
  });
});

// 删除商品
router.delete('/:id', (req, res) => {
  res.json({
    code: 0,
    message: '删除商品成功'
  });
});

module.exports = router;  